<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：账号登录控制器
 * 作者：王楠
 * 创建日期：2017-09-12
 */
class LoginController extends Controller {

    /**
     * 登录
     */
    public function login() {

	if (session('admin_id') && session('admin_username')) {
	    $this->redirect('./index', [], 0, '');
	}
	$system_code = C('SYSTEM_CODE');
	$system_name_abbr = C('SYSTEM_NAME_ABBR');
	$system_name = C('SYSTEM_NAME_ABBR') . C('SYSTEM_NAME');
	$this->assign(array(
	    'system_code' => $system_code,
	    'system_name_abbr' => $system_name_abbr,
	    'system_name' => $system_name,
	));
	$this->display();
    }

    /**
     * 登录提交
     */
//    public function loginsubmit() {
//	if (!IS_POST) {
//	    $this->error('非法访问！');
//	}
//	$username = I('post.username');
//	$password = I('post.password', '', 'htmlspecialchars');
//	if (!empty($password)) {
//	    $password = md5($password);
//	}
//	$map['state'] = array('neq', '3');
//	$map['username'] = $username;
//	$map['password'] = $password;
//	$AdminViewModel = new ViewModel\AdminViewModel();
//	$admin = $AdminViewModel->where($map)->find();
//	if ($admin == null) {
//	    $model = M('chat_admin');
//	    $admin = $model->where($map)->find();
//	}
//	if ($admin != null) {
//	    if ($admin['state'] == '2') {
//		$response = array('ret' => '1002', 'code' => '对不起，账号已被锁定！');
//		return $this->ajaxReturn($response, 'json');
//	    }
//	    //保存登录信息
//	    session('admin_id', $admin['id']);
//	    cookie('admin_id', $admin['id']);
//	    session('admin_username', $admin['username']);
//	    cookie('admin_username', $admin['username']);
//	    session('admin_role_id', $admin['role_id']);
//	    cookie('admin_role_id', $admin['role_id']);
//	    session('admin_role_level', $admin['role_level']);
//	    cookie('admin_role_level', $admin['role_level']);
//	    //管理员登录日志
//	    $log['admin_id'] = session('admin_id');
//	    $log['content'] = "在ip为 " . get_ip() . " 设备上登录。";
//	    $log['type'] = '1';
//	    $log['ctime'] = time();
//	    addlog($log);
//
//	    $response = array('ret' => '1001', 'code' => '登录成功！');
//	} else {
//	    $response = array('ret' => '1002', 'code' => '账号或密码错误！');
//	}
//	return $this->ajaxReturn($response, 'json');
//    }

    /**
     * 登录提交
     */
    public function loginsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$username = I('post.username');
	$password = I('post.password', '', 'htmlspecialchars');
	$app_code = I('post.app_code');
	$type = I('post.type');
	if (!empty($password)) {
	    $password = !empty($app_code) ? $password : md5($password);
	}
	if ($type == '2') {
	    $map['is_agent'] = '2';
	    $map['phone'] = $username;
	    $map['password'] = $password;
	    $model = M('chat_userlogin');
	    $admin = $model->where($map)->find();
	} else {
	    $map['state'] = array('neq', '3');
	    $map['username'] = $username;
	    $map['password'] = $password;
	    $viewModel = new ViewModel\AdminViewModel();
	    $admin = $viewModel->where($map)->find();
	    if ($admin == null) {
		$model = M('chat_admin');
		$admin = $model->where($map)->find();
	    }
	}
	if ($admin != null) {
	    $app_code = $admin['app_flag'];
	    $fxcode = $admin['fxcode'];
	    //保存登录信息
	    if (empty($app_code)) {
		$app_name = '';
	    } else {
		$systemmodel = M('chat_systemset');
		$system = $systemmodel->where(array('app_flag' => $app_code))->order('id desc')->find();
		if (empty($system)) {
		    $response = array('ret' => '1002', 'code' => '对不起，账号所对应的项目不存在！');
		    return $this->ajaxReturn($response, 'json');
		}
		$app_name = $system['app_name'];
	    }
	    if ($admin['state'] == '2') {
		$response = array('ret' => '1002', 'code' => '对不起，账号已被锁定！');
		return $this->ajaxReturn($response, 'json');
	    }
	    session('app_code', $app_code);
            session('fxcode', $fxcode);
	    cookie('app_code', $app_code);
	    session('app_name', $app_name);
	    cookie('app_name', $app_name);
	    session('login_type', $type);
	    cookie('login_type', $type);
	    if ($type == '2') {
		session('admin_id', $admin['id']);
		cookie('admin_id', $admin['id']);
		session('admin_uid', $admin['user_uid']);
		cookie('admin_uid', $admin['user_uid']);
		session('admin_username', $admin['phone']);
		cookie('admin_username', $admin['phone']);
	    } else {
		session('admin_id', $admin['id']);
		cookie('admin_id', $admin['id']);
		session('admin_username', $admin['username']);
		cookie('admin_username', $admin['username']);
		session('admin_role_id', $admin['role_id']);
		cookie('admin_role_id', $admin['role_id']);
		session('admin_role_level', $admin['role_level']);
		cookie('admin_role_level', $admin['role_level']);
	    }
	    //管理员登录日志
	    $log['admin_id'] = session('admin_id');
	    $log['content'] = "在IP为 " . get_ip() . " 设备上登录。";
	    $log['type'] = '1';
	    $log['ctime'] = time();
	    $log['app_flag'] = $app_code;
	    addlog($log);
	    $response = array('ret' => '1001', 'code' => '登录成功！', 'data' => $type);
	} else {
	    $response = array('ret' => '1002', 'code' => '账号或密码错误！', 'data' => $type);
	}
	return $this->ajaxReturn($response, 'json');
    }

    /**
     * 退出登录提交
     */
    public function logoutsubmit() {
	$this->clearsign();
	$this->redirect('./login', [], 0, '跳转中…');
    }

    /**
     * 清除登记信息
     */
    public function clearsign() {
	setcookie('app_code');
	setcookie('login_type');
	setcookie('admin_id');
	setcookie('admin_uid');
	setcookie('admin_username');
	setcookie('admin_role_id');
	setcookie('admin_role_level');
	session_unset();
	session_destroy();
    }

    /**
     * 修改密码
     */
    public function modifypassword() {

	$this->display();
    }

    /**
     * 修改密码提交
     */
    public function modifypasswordsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$passwordold = I('post.passwordold', '', 'htmlspecialchars');
	$password = I('post.password', '', 'htmlspecialchars');
	$passwordconfirm = I('post.passwordconfirm', '', 'htmlspecialchars');
	if (empty($passwordold)) {
	    $this->error('原密码不能为空！');
	}
	if (empty($password)) {
	    $this->error('新密码不能为空！');
	}
	if (empty($passwordconfirm)) {
	    $this->error('确认密码不能为空！');
	}
	if ($password != $passwordconfirm) {
	    $this->error('密码与确认密码不一致！');
	}
	$admin_id = session('admin_id');
	$admin_uid = session('admin_uid');
	$login_type = session('login_type');
	$passwordold = md5($passwordold);
	$password = md5($password);
	if ($login_type == '2') {
	    $model = M('chat_userlogin');
	    $data = $model->where(array('user_uid' => $admin_uid, 'password' => $passwordold))->find();
	} else {
	    $model = M('chat_admin');
	    $data = $model->where(array('id' => $admin_id, 'password' => $passwordold))->find();
	}
	if ($data == null) {
	    $response = array('ret' => '1002', 'code' => '原密码不正确！');
	    return $this->ajaxReturn($response, 'json');
	}
	if ($model->where(array('id' => $admin_id))->save(array('password' => $password))) {
	    $this->clearsign();
	    $response = array('ret' => '1001', 'code' => '修改成功！请重新登录');
	} else {
	    $this->clearsign();
	    $response = array('ret' => '1001', 'code' => '修改成功！请重新登录');
	}
	return $this->ajaxReturn($response, 'json');
    }

}
